﻿using System.Diagnostics;
using System.Windows;
using FileSecurityManager.BizLogic;
using log4net.Config;

namespace FileSecurityManager.UI
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App
    {
        /// <summary>
        /// Handles the Startup event of the application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.StartupEventArgs"/> instance containing the event data.</param>
        private void application_Startup(object sender, StartupEventArgs e)
        {
            // Get Reference to the current Process
            Process thisProc = Process.GetCurrentProcess();
            // Check how many total processes have the same name as the current one
            if (Process.GetProcessesByName(thisProc.ProcessName).Length > 1)
            {
                // If ther is more than one, than it is already running.
                MessageBox.Show(
                    "File Security Manager is already running",
                    "File Security Manager",
                    MessageBoxButton.OK,
                    MessageBoxImage.Warning);

                Current.Shutdown();
                return;
            }

            //Configures logger before running all other code
            XmlConfigurator.Configure();

            // show splash screen
            Splash splashWindow = new Splash();
            splashWindow.Show();

            // show login form
            if (splashWindow.ShowLoginScreen())
            {
                MainWindow main = new MainWindow();
                main.Show();
                splashWindow.Close();
            }
            else
            {
                // exit in case user is not valid
                Current.Shutdown();
                return;
            }
        }
    }
}
